﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kugar.Core.ExtMethod;
using Kugar.Core.Web;
using WS.FlowAuth.Web.Areas.Admin.Filters;
using WS.FlowAuth.Web.Data.EF;

namespace WS.FlowAuth.Web.Areas.Admin.Controllers
{
    [UserAuthority]
    public class AuthLogController : AdminBaseController
    {
        // GET: Admin/AuthLog
        public ActionResult List()
        {
            var startDt = Request.GetDateTimeNullable("StartDt", "yyyy-MM-dd", null);
            var endDt = Request.GetDateTimeNullable("EndDt", "yyyy-MM-dd", null);
            var keyword = Request.GetString("keyword", "");
            var pageIndex = Request.GetInt("PageIndex", 1);

            if (endDt.HasValue)
            {
                endDt = endDt.SetEndTime();
            }

            if (startDt.HasValue)
            {
                startDt = startDt.SetStartTime();
            }
                
            var query = from log in Context.log_AuthCode
                join code in Context.base_UserCode on log.UserCodeID equals code.UserCodeID
                where (keyword == "" ||
                       code.UserCode.Contains(keyword) || 
                       code.CustomerName.Contains(keyword) ||
                       log.OptCode.Contains(keyword) || 
                       log.MachineCode.Contains(keyword) ||
                       log.FlowNo.Contains(keyword)
                       
                       ) &&
                      (startDt == null || log.CreateDt >= startDt) &&
                      (endDt == null || log.CreateDt <= endDt)
                orderby log.CreateDt descending
                select log;


            var lst = query.GetPagedList(pageIndex, 20);

            if (lst.HasData())
            {
                var codeIDs = lst.Data.Select(x => x.UserCodeID).ToArray();

                ViewBag.UserCodes = Context.base_UserCode.Where(x => codeIDs.Contains(x.UserCodeID)).ToArrayEx();
            }
            else
            {
                ViewBag.UserCodes = new base_UserCode[0];
            }

            //foreach (var item in lst.Data)
            //{
            //    item.OptCode=WS.FlowAuth.Converter.Converter.ConvertOpCode(item.OptCode);
            //}

            ViewBag.List = lst;
            ViewBag.StartDt = startDt;
            ViewBag.EndDt = endDt;
            ViewBag.Keyword = keyword.ToStringEx();


            return View();
        }

        public ActionResult Detail()
        {
            var logID = Request.GetInt("logID");

            if (logID<=0)
            {
                return HttpNotFound();
            }

            var model = Context.log_AuthCode.FirstOrDefault(x => x.LogID == logID);

            if (model==null)
            {
                return HttpNotFound();
            }

            try
            {
                var newOptData= WS.FlowAuth.Converter.Converter.ParseOpData(model.OptCode, Convert.FromBase64String(model.OptData));
                model.OptData = newOptData;
            }
            catch (Exception e)
            {
                
            }

            
            model.OptCode = WS.FlowAuth.Converter.Converter.ConvertOpCode(model.OptCode);

            ViewBag.Info = model;


            return View();
        }
    }
}